var refreshNav = false;
layui.use(['layer', 'jquery', 'element', 'form', 'laytpl', 'navigate', 'info'], function() {
	var $ = layui.jquery
		, layer = layui.layer
		, form = layui.form
		, element = layui.element
		, laytpl = layui.laytpl
		, navigate = layui.navigate
		, info = layui.info;
			
	initUserInfo();  //获取用户信息
	initNav();  //获取导航栏
	
	//路由注册
	Q.reg('home',function(){
		load('home');
	}).reg('system',function(path){
		load('system/'+path);
	}).init({
		index: 'home'		
	});
	
	//首次访问的url如果带有锚点激活对应的item
	if (location.hash != "") {
		navigate.activeNav(location.hash.substring(2));
	}
	
	//点击导航切换页面时不刷新导航,其他方式切换页面要刷新导航
	element.on('nav(index-nav)', function(elem){
		refreshNav = false;
		if(document.body.clientWidth<=750){
			navigate.switchNav(true);
		}
	});
	
	//修改密码表单提交事件
	form.on('submit(pswSubmit)', function(data){
		data.field.token = getToken();
		data.field._method = $("#pswForm").attr("method");
		layer.load(1);
		$.post("api/user/psw.json", data.field, function(data){
			if(data.code==200){
				layer.msg(data.msg,{icon: 1});
				setTimeout(function() {
					info.loginOut();
				}, 1500);
			}else{
				layer.closeAll('loading');
				layer.msg(data.msg,{icon: 2});
			}
		}, "JSON");
		return false;
	});
	
	form.verify({
		  psw2: function(value, item){
			var newPsw1 = $("#pswForm input[name=newPsw]").val();
		    if(value!=newPsw1){
		      return '两次输入密码不一致';
		    }
		  },
		  pass: [
		    /^[\S]{6,12}$/
		    ,'密码必须6到12位，且不能出现空格'
		  ]
	});
				
	if(getCurrentUser()==null){
		// location.replace("login.html");
	}
	
	//切换导航栏按钮点击事件
	$("#switchNav").click(function(){
		var sideNavExpand = !$('body').hasClass('nav-mini');
		navigate.switchNav(!sideNavExpand);
	});
	
	//手机遮罩层点击事件
	$('.site-mobile-shade').click(function(){
		navigate.switchNav(true);
	});
	
	//获取用户信息
	function initUserInfo(){
		try {
			var user = getCurrentUser();
			//$("#userHead").attr("src", user.);
			$("#userNickName").text(user.userNickname);
		} catch (e) {
			console.log(e.message);
		}
	}
	
	//获取左侧导航栏
	function initNav(){
		var indexNavStr = sessionStorage.getItem("index-nav");
		var indexNav = JSON.parse(indexNavStr);
		if(indexNav==null){
			$.get("api/menu.json", {
				token : getToken()
			}, function (data) {
				if(200==data.code){
					sessionStorage.setItem("index-nav",JSON.stringify(data.menus));
					initNav();
				}else if(401==data.code){
					console.log(data.message);
					layer.msg(data.message,{icon: 2});
					setTimeout(function() {
						info.loginOut();
					}, 1500);
				}else{
					layer.msg("获取导航失败，请刷新页面",{icon: 2});
				}
			},"json");
		}else{
			laytpl(sideNav.innerHTML).render(indexNav, function(html){
				$("#index-nav").html(html);
				element.render('nav', 'index-nav');
			});
		}
	}
	
	//异步加载子页面
	function load(path) {
		// if(refreshNav){
		//  	navigate.activeNav(path);
		//}
		//refreshNav = true;
		$("#main-content").load("views/" + path +".html",function(){
			element.render('breadcrumb');
			form.render('select');
		});
	}
	
});